package com.flower.controller.farmer;

import com.flower.annotation.Log;
import com.flower.domain.entity.Farmer;
import com.flower.service.FarmerService;
import com.flower.utils.AjaxResult;
import com.flower.utils.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 花农端个人信息
 */
@RestController
@RequestMapping("/farmer/account")
@Slf4j
public class FarmerController {

    @Autowired
    private FarmerService farmerService;

    /**
     * 获取个人信息
     * @return
     */
    @GetMapping("/info")
    public AjaxResult getUserInfo() {
        Farmer farmer = farmerService.getUserInfo();
        return AjaxResult.success(farmer);
    }

    /**
     * 修改个人信息
     * @param farmer
     * @return
     */
    @Log(name = "修改花农个人信息")
    @PutMapping("/update")
    public AjaxResult updateUserInfo(@RequestBody Farmer farmer) {
        log.info("修改用户信息，参数：{}", farmer);
        Long userId = UserContext.getUserId();
        farmer.setFarmerId(userId.intValue());
        farmer.setUpdateTime(LocalDateTime.now());
        farmerService.updateUserInfo(farmer);
        return AjaxResult.success();
    }
}
